/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.basic.dao.impl;

import java.util.List;

import com.eelpo.cmmp.basic.dao.ISystemConfigManageDao;
import com.eelpo.cmmp.basic.dao.entity.TSystemConfig;
import com.eelpo.cmmp.basic.dao.vo.SystemConfig;
import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Order;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 * 
 * 系统配置数据访问具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class SystemConfigManageDao extends BasicCommonDao implements ISystemConfigManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#getSystemConfigById(java.lang
	 * .String)
	 */
	@Override
	public TSystemConfig getSystemConfigById(String id) throws DataAccessException {
		return find(TSystemConfig.class, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#getSystemConfigByKey(java.
	 * lang.String)
	 */
	@Override
	public TSystemConfig getSystemConfigByKey(final String key) throws DataAccessException {
		List<?> list = findByNamedQuery("SystemConfig.findByKey", key);
		return (TSystemConfig) (list.isEmpty() ? null : list.get(0));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String key) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TSystemConfig.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("configKey", key));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#checkExistByUpdate(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public boolean checkExistByUpdate(String id, String key) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TSystemConfig.class);
		criteria.result(Result.count());
		criteria.add(Expression.ne("id", id));
		criteria.add(Expression.eq("configKey", key));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#addSystemConfig(com.eelpo
	 * .cmmp.basic.dao.entity.TSystemConfig)
	 */
	@Override
	public String addSystemConfig(TSystemConfig config) throws DataAccessException {
		persist(config);
		return config.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#updateSystemConfig(com.eelpo
	 * .cmmp.basic.dao.entity.TSystemConfig)
	 */
	@Override
	public void updateSystemConfig(TSystemConfig config) throws DataAccessException {
		merge(config);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#deleteSystemConfig(java.lang
	 * .String)
	 */
	@Override
	public void deleteSystemConfig(String id) throws DataAccessException {
		remove(getReference(TSystemConfig.class, id));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#getSystemConfigsPaing(com.
	 * eelpo.cmmp.basic.dao.vo.SystemConfig, int, int)
	 */
	@Override
	public List<TSystemConfig> getSystemConfigsPaing(SystemConfig config, int first, int max) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TSystemConfig.class);
		criteria.add(Expression.like("configKey", config.getConfigKey()));
		criteria.add(Expression.like("configValue", config.getConfigValue()));
		criteria.add(Expression.like("description", config.getDescription()));
		criteria.add(Expression.like("hint", config.getHint()));
		criteria.order(Order.order(config.getSort()));
		criteria.setFirstResult(first);
		criteria.setMaxResults(max);
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.eelpo.cmmp.basic.dao.ISystemConfigDao#getSystemConfigsCount(com.
	 * eelpo.cmmp.basic.dao.vo.SystemConfig)
	 */
	@Override
	public int getSystemConfigsCount(SystemConfig config) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TSystemConfig.class);
		criteria.result(Result.count());
		criteria.add(Expression.like("configKey", config.getConfigKey()));
		criteria.add(Expression.like("configValue", config.getConfigValue()));
		criteria.add(Expression.like("description", config.getDescription()));
		criteria.add(Expression.like("hint", config.getHint()));
		return getResultCount(criteria);
	}
}
